//
//  HEIMMessageModel.m
//  HealthElderly
//
//  Created by 吴欧 on 2020/11/27.
//  Copyright © 2020 honglutec. All rights reserved.
//

#import "HEIMMessageModel.h"
#import "HEIMTextCell.h"
#import "HEIMImageViewCell.h"
#import "HEIMAudioCell.h"
#import "HEIMAudioObject.h"
#import "HEIMConsulReportCell.h"
#import "HEIMTWFormCell.h"
#import "HETWFormObject.h"
#import "HEIMVideoCell.h"
#import "HEIMProductCell.h"
#import "HEIMProductObject.h"
#import "HEIMCommentCell.h"
#import "HEIMRoomInAndoutCell.h"
#import "HEIMInfoPageCell.h"
#import "HEIMInfoVideoPageCell.h"

@implementation HEIMMessageModel

- (instancetype)initWithMessage:(HEIMMessage *)message
{
    if (self = [self init]) {
        _message = message;
        
        [self dealMessage];
    }
    return self;
}

- (void)dealMessage {
    
    [self getIsOwner];
    [self getCellType];
    [self getCellIdentifier];
    
    [self updateLayoutConfig];
    [self computeContentWidth];
    [self computeUserHeadOriginX];
    [self computeContentWarpOriginX];

    [self getWarpColor];
    [self getWarpTextColor];
    [self getNameStringSize];
    
    if (_message.type == HEIMMessageTypeImage) {
        [self getKNPhotoItems];
    }
    
    if (_message.type == HEIMMessageTypeAudio) {
        [self getAudioAnimotionImages];
    }
}

- (void)getIsOwner {
 
    _isOwn = [[UserManager sharedUserManager].curUserInfo.id isEqualToString: _message.from];
    _message.isOwn = _isOwn;
//    if (_message.actor == HEActorTypeUser) {
//        _isOwn = YES;
//    }else if (_message.actor == HEActorTypeDoctor) {
//        _isOwn = NO;
//    }
}

- (Class)getCellType {
    switch (_message.type) {
        case HEIMMessageTypeText:
        case HEIMMessageTypeAT:
        case HEIMMessageTypeGroupNotif:
            _cellClass = HEIMTextCell.class;
            break;
        case HEIMMessageTypeImage:
            _cellClass = HEIMImageViewCell.class;
            break;
        case HEIMMessageTypeAudio:
            _cellClass = HEIMAudioCell.class;
            break;
        case HEIMMessageTypeConsulReport:
            _cellClass = HEIMConsulReportCell.class;
            break;
        case HEIMMessageTypeTWForm:
            _cellClass = HEIMTWFormCell.class;
            break;
        case HEIMMessageTypeProduct:
            _cellClass = HEIMProductCell.class;
            break;
        case HEIMMessageTypeVideo:
            _cellClass = HEIMVideoCell.class;
            break;
        case HEIMMessageTypeComment:
            _cellClass = HEIMCommentCell.class;
            break;
        case HEIMMessageTypeInfoPage:
            _cellClass = HEIMInfoPageCell.class;
            break;
        case HEIMMessageTypeInfoVideo:
            _cellClass = HEIMInfoVideoPageCell.class;
            break;
            
            
        case HEIMMessageTypeSystemRoomInAndOutNotice:
        case HEIMMessageTypeGroupNotice:
            _cellClass = HEIMRoomInAndoutCell.class;
            break;
            

        default:
            _cellClass = HEIMTextCell.class;

            break;
    }
    return _cellClass;
}

- (void)getCellIdentifier {
    NSString *ownerSuffix = @"";
    NSString *imSuffix = @"";
    
    if(self.isOwn) {
        ownerSuffix = HECellIdentifierOwnerSelf;
    }else {
        ownerSuffix = HECellIdentifierOwnerOther;
    }
    
    // 是系统消息
    if (_message.type == HEIMMessageTypeSystemRoomInAndOutNotice ||
        _message.type == HEIMMessageTypeGroupNotice) {
        ownerSuffix = HECellIdentifierSystem;
    }
    
    if (self.message.modelType == HEIMUserModelTypeTeamIM ||
        self.message.modelType == HEIMUserModelTypeRoom ||
        self.message.modelType == HEIMUserModelTypeSickbed ||
        self.message.modelType == HEIMUserModelTypeGroup) {
        imSuffix = HECellIdentifierGroup;
    }else {
        imSuffix = HECellIdentifierSingle;
    }
     
    _cellIdentifier = [NSString stringWithFormat:@"%@_%@_%@",NSStringFromClass([self getCellType]), ownerSuffix, imSuffix];
}

/// 计算cell的内容宽度，不包括内容容器左右边距
- (void)computeContentWidth {
    
    CGFloat fixOffest = (15 + 50) * 2;
    CGFloat margin = 20;
    CGFloat warp = KScreenWidth - fixOffest - margin;
    
    switch (_message.type) {
        case HEIMMessageTypeText:
        case HEIMMessageTypeGroupNotif:
        {
            _contentWidth = ceilf([_message.text widthForFont:DYFont(16)]);
            // warp是整个气泡的宽度，但是计算气泡里的内容宽度时需要再减去内容左右边距20
            if ((_contentWidth + margin) > warp) {
                _contentWidth = warp - margin;
            }
        }
            break;
            
        case HEIMMessageTypeImage:
        {
//            CGFloat fixOffest = (15 + 50) * 2;
//            CGFloat margin = 20;
//            CGFloat warp = KScreenWidth - fixOffest - margin;
            
            HEIMImageObject *image = (HEIMImageObject *)_message.messageObject;
            
            // warp是整个气泡的宽度，但是计算气泡里的内容宽度时需要再减去内容左右边距20 warp - margin
            if (image.imageArray.count == 1) {
                _imageItemWidth = (warp - margin) / 2.0;
                _contentWidth = _imageItemWidth;
            }else if (image.imageArray.count == 2) {
                _imageItemWidth = ((warp - 10 - margin) / 2.0);
                _contentWidth = warp - 10;
            }else {
                _imageItemWidth = (warp - 20 - margin) / 3.0;
                _contentWidth = warp - 20;
            }
            
            NSInteger row = ceilf(image.imageArray.count / 3.0) ;
            _contentHeight = _imageItemWidth * row + 10 * (row-1);

        }
            break;
        case HEIMMessageTypeAudio:
        {
            HEIMAudioObject *audio = (HEIMAudioObject *)_message.messageObject;
            CGFloat lbWidth = [[NSString stringWithFormat:@"%ld", audio.duration] widthForFont:DYFont(16)];
            _contentWidth = lbWidth + 10 + 80;
            
        }
            break;
        case HEIMMessageTypeConsulReport:
        {
            CGFloat minWidth = [@"点击查看报告内容>>" widthForFont:DYFont(14)] + 5 * 2;
            NSString *titl = [NSString stringWithFormat:@"咨询内容：%@", _message.title];
            CGFloat lbWidth = [titl widthForFont:DYFont(16)] + 5 * 2;
            
            if (lbWidth > minWidth) {
                _contentWidth = lbWidth;
            }else {
                _contentWidth = minWidth;
            }
             
            // warp是整个气泡的宽度，但是计算气泡里的内容宽度时需要再减去内容左右边距20
            if (_contentWidth > warp) {
                _contentWidth = warp - margin;
            }
        }
            break;
        case HEIMMessageTypeTWForm: {
            _contentWidth = warp;
        }
            break;
        case HEIMMessageTypeVideo: {
            _contentWidth = warp * 0.6;
        }
            break;
        case HEIMMessageTypeProduct:
        {
            HEIMProductObject *product = (HEIMProductObject *)_message.messageObject;
            
//            CGFloat nameWidth = ceilf([product.product_name widthForFont:DYFont(16)]);
//            CGFloat iconWidth = 70 + 10;
//
//            _contentWidth = nameWidth + iconWidth;
//
//            // warp是整个气泡的宽度，但是计算气泡里的内容宽度时需要再减去内容左右边距20
//            if ((_contentWidth + margin) > warp) {
//                _contentWidth = warp - margin;
//            }
//            _contentWidth = _contentWidth - iconWidth;
            _contentWidth = 160;
        }
            break;
        case HEIMMessageTypeComment:
        {
            _contentWidth = ceilf([_message.text widthForFont:DYFont(16)]);
            // warp是整个气泡的宽度，但是计算气泡里的内容宽度时需要再减去内容左右边距20
            if ((_contentWidth + margin) > warp) {
                _contentWidth = warp - margin;
            }
        }
            break;
        case HEIMMessageTypeInfoPage:
        case HEIMMessageTypeInfoVideo:
        {
            HEIMInfoPageObject *infoPage = (HEIMInfoPageObject *)_message.messageObject;
            _contentWidth = ceilf([infoPage.title widthForFont:DYBlodFont(16)]) + 50 + 10;
            // warp是整个气泡的宽度，但是计算气泡里的内容宽度时需要再减去内容左右边距20
            if ((_contentWidth + margin) > warp) {
                _contentWidth = warp - margin;
            }
        }
            break;

        default:
            
            break;
    }
}


- (void)getKNPhotoItems {
    _KNPhotoItems = @[].mutableCopy;
    HEIMImageObject *image = (HEIMImageObject *)_message.messageObject;
    for (HEIMImageObject *object in image.imageArray) {
        KNPhotoItems *items = [[KNPhotoItems alloc] init];
       items.url = object.url.absoluteString;
       [_KNPhotoItems addObject:items];
    }
}

//- (CGFloat)contentHeight {
//    if (!_contentHeight) {
//
//    }
//    return _contentHeight;
//}


- (void)computeUserHeadOriginX {
    if (_isOwn) {
        _userHeadOriginX = KScreenWidth - 50 -15;
    }else {
        _userHeadOriginX =  15;
    }
}

- (void)computeContentWarpOriginX {
    if (_isOwn) {
        if (_message.type == HEIMMessageTypeVideo ||
            _message.type == HEIMMessageTypeTWForm) {
            _warpOriginX = KScreenWidth - (self.contentWidth) - (50 + 15 + 10);
        }else if (_message.type == HEIMMessageTypeConsulReport){
            _warpOriginX = KScreenWidth - (self.contentWidth + 20) - (50 + 15 + 10) - 10;
        }else {
            _warpOriginX = KScreenWidth - (self.contentWidth + 20) - (50 + 15 + 10);
        }
        _activityIndOriginX = _warpOriginX - 20 - 10;
    }else {
        _warpOriginX = 50 + 15 + 10;
        _activityIndOriginX = _warpOriginX + _contentWidth + 20 + 10;
    }
}

- (void)getWarpColor {
    if (_isOwn) {
        _warpColor = CNavBgColor;
    }else {
        _warpColor = KWhiteColor;
    }
}

- (void)getWarpTextColor {
    
    switch (_message.type) {
        case HEIMMessageTypeAudio:
        {
            if (_isOwn) {
                _warpTextColor = KWhiteColor;
            }else {
                _warpTextColor = CNavLightBlackColor;
            }
        }
            break;
            
        default:
        {
            if (_isOwn) {
                _warpTextColor = KWhiteColor;
            }else {
                _warpTextColor = CNavLightBlackColor;
            }
        }
            break;
    }
}

- (void)getAudioAnimotionImages {
    _audioAnimotionImages = @[].mutableCopy;
    NSArray *imgs = @[];
    if (_isOwn) {
        imgs = @[@"voice_playing_lt_003", @"voice_playing_lt_001", @"voice_playing_lt_002", @"voice_playing_lt_003"].mutableCopy;
    }else {
        imgs = @[@"icon_receiver_voice_playing", @"icon_receiver_voice_playing_001", @"icon_receiver_voice_playing_002", @"icon_receiver_voice_playing_003"].mutableCopy;
    }
    
    for (NSString * animateName in imgs) {
        UIImage * animateImage = IMAGE_NAMED(animateName);
        [_audioAnimotionImages addObject:animateImage];
    }
}


- (void)updateLayoutConfig {
    if (_message.modelType == HEIMUserModelTypeTeamIM ||
        _message.modelType == HEIMUserModelTypeRoom ||
        _message.modelType == HEIMUserModelTypeSickbed ||
        _message.modelType == HEIMUserModelTypeGroup) {
        _shouldShowNickName = !self.isOwn;
    }else {
        _shouldShowNickName = NO;
    }
}

- (void)getNameStringSize {
    
    CGFloat fixOffest = (15 + 50) * 2;
    CGFloat margin = 20;
    CGFloat warp = KScreenWidth - fixOffest - margin;
     
    _nameSize = CGSizeMake(warp, 20);
}

    
//- (BOOL)isEqual:(id)object
//{
//    if (![object isKindOfClass:[NIMMessageModel class]])
//    {
//        return NO;
//    }
//    else
//    {
//        NIMMessageModel *model = object;
//        return [self.message isEqual:model.message];
//    }
//}

@end


